package praktikumid.w09.p4;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * Center panel where we draw shapes.
 * @author Ago
 *
 */
public class DrawPanel extends JPanel {
	/**
	 * Constructor, does initialize the panel.
	 * It only sets the background white.
	 */
	public DrawPanel() {
		setBackground(Color.WHITE);
	}
	
	/**
	 * Draws oval with specified left upper point.
	 * @param x x-coordinate of the left upper point
	 * @param y y-coordinate of the left upper point
	 */
	public void drawOval(int x, int y) {
		Graphics g = getGraphics();
		g.drawOval(x, y, 100, 50);
		// we need to dispose graphics
		// if we use getGraphics()
		g.dispose();
	}
	
	/**
	 * Draws rectangle.
	 * @param x x-coordinate of the left upper point
	 * @param y y-coordinate of the left upper point
	 */
	public void drawRect(int x, int y) {
		Graphics g = getGraphics();
		g.drawRect(x, y, 100, 50);
		g.dispose();
	}
	
	/**
	 * Draws line.
	 * @param x x-coordinate of the starting point
	 * @param y y-coordinate of the starting point
	 */
	public void drawLine(int x, int y) {
		Graphics g = getGraphics();
		g.drawLine(x, y, 0, 0);
		g.dispose();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// here we extends paintComponent method
		// in order to draw something on the panel
		// after every repaint() (e.g. after resize,
		// after creating the panel etc).
		// Other shapes we draw within draw* methods
		// are all erased when the window is resized.
		// The reason is, that after resize, paint()
		// method is called, which in return calls
		// paintComponent method. 
		
		// let's call paintComponent on the superclass
		// just in case
		super.paintComponent(g);
		g.setColor(Color.BLUE);
		g.drawLine(100, 0, 0, 100);
		// there's no need to dispose Graphics here.
	}
}
